<?php
	namespace KJ;
	use KJ\Kj;
	class Page{
		private $data;
		private $count;
		private $pageSize;
		private $page;
		private $p;
		public function __construct($data , $count , $pageSize){
			$p = isset($_GET[Kj :: $app->config['PAGE']]) ? $_GET[Kj :: $app->config['PAGE']] : 1;
			$this->data = $data;
			$this->count = $count;
			$this->pageSize = $pageSize;
			$this->page = ceil($count/$pageSize);
			if($p < 1){
				$this->p = 1;
			}else if($p > $this->page){
				$this->p = $this->page;
			}else{
				$this->p = $p;
			}
			
			
		}
		public function pagination(){
			
			$arr = array();
			$result = array();
			$pageSize = $this->pageSize;
			$count = $this->count;
			$data = $this->data;
			$page = $this->page;
			for($i = 1 ; $i <= $this->page ; $i++){
				for($j = $pageSize*($i-1) ; $j < $pageSize*($i) ; $j++){
					if(isset($data[$j])){
						$arr[$i][] = $data[$j];
					}
				}
			}
			$prev = ($this->p-1)<1 ? $this->p : $this->p-1;
			$next = $this->p+1>$this->page ? $this->page : $this->p+1;
			// 这里改一下防止空数组
			$result['data'] = empty($arr[$this->p]) ? array() : $arr[$this->p] ;
			//
			$result['html'] = '<div id="page">

			<a href="index.php?'.Kj :: $app->config['VAR_MODULE'].'='.MODULE_NAME.'&'.Kj :: $app->config['VAR_CONTROLLER'].'='.CONTROLLER_NAME.'&'.Kj :: $app ->config['VAR_ACTION'].'='.ACTION_NAME.'&'.Kj :: $app -> config['PAGE'].'='.$prev.'">上一页</a>
			<span>'.$this->p.'</span>
			<a href="index.php?'.Kj :: $app->config['VAR_MODULE'].'='.MODULE_NAME.'&'.Kj :: $app->config['VAR_CONTROLLER'].'='.CONTROLLER_NAME.'&'.Kj :: $app ->config['VAR_ACTION'].'='.ACTION_NAME.'&'.Kj :: $app -> config['PAGE'].'='.$next.'">下一页</a>
			<span style="width:60px;border:1px solid orange;">共'.$this->page.'页</span>
			<i>到：</i>
			<form id="submit" action="index.php?'.Kj :: $app->config['VAR_MODULE'].'='.MODULE_NAME.'&'.Kj :: $app->config['VAR_CONTROLLER'].'='.CONTROLLER_NAME.'&'.Kj :: $app ->config['VAR_ACTION'].'='.ACTION_NAME.'" method="get">
				<input type="hidden" name="'.Kj :: $app->config['VAR_MODULE'].'" value="'.MODULE_NAME.'"/>
				<input type="hidden" name="'.Kj :: $app->config['VAR_CONTROLLER'].'" value="'.CONTROLLER_NAME.'"/>
				<input type="hidden" name="'.Kj :: $app->config['VAR_ACTION'].'"  value="'.ACTION_NAME.'"/>
				<input type="text" class="p" name="'.Kj :: $app->config['PAGE'].'"/>
				<input type="submit" value="确认" class="submit" />
			</from>
			<i>页</i>
			
					</div>';
			$result['js'] = '
			<script>
				window.onload = function(){
					var oSub = document.getElementById("submit");
					var p = document.getElementsByClassName("p")[0];
					oSub.onsubmit = function(){
						if(p.value < 1 || p.value > '.$this->page.'){
							alert("页码不存在");
							p.value = "";
							return false;
						}
					}
				}
			</script>';
			
			
			$result['css'] = '<style>#page{
				width: 330px;
				height: 30px;
				line-height: 30px;
			}
			#page a{
				text-decoration: none;
				color: black;
				text-align : center;
				width: 50px;
				height: 30px;
				line-height: 30px;
				border:1px solid orange;
				border-radius: 3px;
				float: left;
			}
			#page a:hover{
				background: orange;
			}
			#page span{
				
				width: 30px;
				height: 30px;
				text-align: center;
				line-height: 30px;
				border:1px solid black;
				border-radius: 3px;
				float: left;
				margin-left: 3px;
				margin-right: 3px;
			}
			#page .p{
				width: 30px;
				height: 30px;
				line-height: 30px;
				border: none;
				outline: none;
				border-radius: 3px;
				border:1px solid black;
				float: left;
				text-align:center;
			}
			#page .submit{
				width: 30px;
				height: 30px;
				border:none;
				border-radius:3px;
				background:orange;
				margin-left:10px;
				outline:none;
			}
			#page i{
				float: left;
			}</style>';
			
			return $result;
		}
	}
?>

